// 药房管理
<template comments>
  <div class="pharmacy">
    <TopManu RegistrationFee="b8"   :pharmacyStatus="true" @handcreate="handcreate" :signOut="true" @handleQuit="$router.go(-1)"></TopManu>
    <div class="tabsB">
      <el-tabs v-model="params.tabIndex" type="card" @tab-click="tabsIndexClick">
      <el-tab-pane label="待配药" name="1"></el-tab-pane>
      <el-tab-pane label="已配药" name="2"></el-tab-pane>
       <!-- <el-tab-pane label="待退药" name="3"></el-tab-pane>
        <el-tab-pane label="已退药" name="4"></el-tab-pane> -->
    </el-tabs>
    </div>
    <div class="search">
      <div class="s1">
   
          <el-date-picker
           @change="searchB"
            v-model="params.start_time"
             v-if="params.tabIndex=='1'"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="开始日期"
           
          >
          </el-date-picker>
                    <el-date-picker
                    v-if="params.tabIndex=='2'"
           @change="searchB"
            v-model="params2.start_time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="开始日期"
           
          >
          </el-date-picker>
        
         <el-date-picker
           @change="searchB"
           v-if="params.tabIndex=='2'"
            v-model="params.end_time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="结束"
          
          >
          </el-date-picker>
           <el-button  v-if="params.tabIndex=='1'" @click="yijianpeiyao()">一键配药</el-button>
           <el-button  v-if="params.tabIndex=='2'" @click="yijiantuiyao()">一键退药</el-button>
        <!-- <el-button @click="searchB()">查询</el-button> -->
           
      </div>
    
    </div>

    <div class="tablesB">
      <el-table  :data="data" border 
      @selection-change="handleSelectionChange">
       <el-table-column
       
      type="selection"
      width="55">
    </el-table-column>
        <el-table-column type="index" width="70" label="序号">
        </el-table-column>

        <template v-for="(item, index) in tableTitle">


                                    <el-table-column
                       
                        v-if="item.prop == 'get_fill'"
                        :prop="item.prop"
                     
                        :label="item.name"
                        :key="index"
                      >
                        <template slot-scope="scope">
                          <span>
                         <el-select
                        v-model="scope.row.fill_id"
                        size="mini"
                        placeholder="批号"
                      >
                        <el-option
                          v-for="item in scope.row.get_fill"
                          :key="item.fill_no"
                          :label="item.fill_no"
                          :value="item.id"
                        >
                        </el-option>
                      </el-select>
                          </span>
                        </template>
                      </el-table-column>
                      <el-table-column v-else-if="item.name == '操作'"    :prop="item.prop"
                     
                        :label="item.name"
                        :key="index">
          <template slot-scope="scope">
            <div>
              
         
              <el-button
              
                size="mini"
                type="danger"
                @click="handleEdit(scope.$index, scope.row, 1)"
                >详情</el-button
              >
       
        
            </div>
          </template>
        </el-table-column>
            <el-table-column v-else :width="item.width" :prop="item.prop" :label="item.name" :key="index+item"> </el-table-column>
           </template>
        <!-- <el-table-column label="操作" width="250" fixed="right">
          <template slot-scope="scope">
            <div>
              
              <el-button
                v-if="params.tabIndex === '1'"
                size="mini"
                type="danger"
                @click="handleEdit(scope.$index, scope.row, 2)"
                >配药</el-button
              >
              <el-button
                v-if="params.tabIndex === '2'"
                size="mini"
                type="danger"
                @click="handleEdit(scope.$index, scope.row, 1)"
                >详情</el-button
              >
       
              <el-button
                v-if="params.tabIndex === '3'"
                size="mini"
                type="danger"
                @click="handleEdit(scope.$index, scope.row, 4)"
                >退药</el-button
              >
            </div>
          </template>
        </el-table-column> -->
      </el-table>
        <div style="margin-top:20px;width:30%;float:left">
           <el-pagination
          
            :background="true"
            :current-page="params.page"
            :page-size="params.limit"
            layout="prev, pager, next"
            @current-change="handlePagination"
            :total="params.pageCount">
            </el-pagination>
            
      </div>
     
    </div>


      <el-dialog
        :title="dialogTitle"
        :visible.sync="ZY_Detail_Show"
        v-if="ZY_Detail_Show"
        :fullscreen="true"
        :destroy-on-close="true"
        width="30%"
        :before-close="()=>{this.ZY_Detail_Show=!this.ZY_Detail_Show}">
             <MZdetail  :thatF='this'></MZdetail>
      
      </el-dialog>
  </div>
</template>

<script>
let t;
import MZdetail from './common/detail'
import TopManu from "@/components/TopMenu";
import { mapState,mapActions } from "vuex";
import { getDrugHouse,getDrugHouseDetail,batchGenDrug,getDrugList,batchRefundDrug } from "@/api/patient";
// getDict_dataKuFangList
export default {
  name:"pharmacy_ZY",
  data() {
    return {
      ZY_Detail_Show:false,
      tableTitle:[],
      tableTitle1: [
            {
          prop: "name",
          name: "药品名称",
          type: 1,
         
        },
        {
          prop: "specification",
          name: "规格",
          type: 1,
        
        },
          {
           prop: "money",
          name: "费用",
          type: 1
          
        },
        {
          prop: "num",
          name: "数量",
          type: 1
        },
         {
           prop: "unit",
          name: "单位",
          type: 1
          
        }, 
        {
           prop: "get_fill",
          name: "批号",
          type: 1
          
        },
        
           {
           prop: "id",
          name: "操作",
          type: 1
          
        },
      ],
        tableTitle2: [
            {
          prop: "business_name",
          name: "药品名称",
          type: 1,
         
        },
        {
          prop: "start_time",
          name: "开始时间",
          type: 1,
        
        },
          {
           prop: "end_time",
          name: "结束时间",
          type: 1
          
        },
        {
          prop: "money",
          name: "总价",
          type: 1
        },
         {
           prop: "unit",
          name: "单位",
          type: 1
          
        }, 
        {
           prop: "fill_no",
          name: "批号",
          type: 1
          
        },
           {
           prop: "id",
          name: "操作",
          type: 1
          
        },
        
      ],
      dialogTitle:'',
      params: {
        loading:false,
        tabIndex: "1",
        page:1,
        limit:10,
        value1: "",
       end_time:'',
        keyword: "",
        start_time: "",
         pageCount:0,
      },
      params2:{
          page:1,
        limit:20,
        pageCount:0,
        start_time:""
      },
    
      data: [],
      data2:[],
      cfId:'',
      item2:[],
    };
  },
  components: {
    TopManu,
    MZdetail,
  },
   computed: {
    ...mapState({
      KuFangList: state => state.dictBai.KuFangList,
      bingQuList: state => state.dictBai.KuFangList,
    }),
  },
  mounted() {
    t = this;
               let date = new Date()
        let yyyy = date.getFullYear()
        let MM = (date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : (date.getMonth() + 1)
        let dd = date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate()
     
        let curDay = yyyy + '-' + MM + '-' + dd 
          this.params.start_time=curDay
          this.params.end_time=curDay
  //  t.tableTitle = t.$baseBai["hospitalization"]['pharmacy_ZY_list1'];

    // if(t.KuFangList.length==0){
    //     t['dictBai/getDict_dataKuFangList']({})
    // }
    
    //   if(t.bingQuList.length==0){
    //     t['dictBai/getSysbuildGetAllIcu']({})
    // }
      t.getList()
   
  },
  methods: {
     ...mapActions([
				'dictBai/getDict_dataKuFangList',
        
        'pharmacy_ZY/getH_pharmacyGetDetail2',
        'pharmacy_ZY/getH_pharmacyGetDetail',
        'dictBai/getSysbuildGetAllIcu'
       
			]),
    //    resetSearchB(){

   
       
    //   this.getList()
    //   },
      handcreate(type){
          console.log(type)
      

        if(type==5){
          
          this.$router.push({name:'drugCollar'})
        }else if(type==2){
           this.$router.push({name:'zy_statistic',query:{type:'zy'}})
        } else{
                this.$router.push({name:'pharmacy_STOCK',query: {
          house_id: 2, 
        }})
        }

           
      },
    handleEdit(...item){
     if(item[2]===1){
        t.dialogTitle='详情信息'
       var data={
         business_id:item[1].business_id,
         start_time:this.params.start_time,
         page:this.params2.page,
         limit:this.params2.limit

       }
       this.item2=item
        getDrugHouseDetail(data).then(res=>{
            if(res.code==200){
              console.log(res)
              this.dataPeiYao=res.data.data.lists
              this.params2.pageCount=res.data.data.count
              t.ZY_Detail_Show=!t.ZY_Detail_Show;
              }
        })
     
      }
     // t['pharmacy_MZ/getDrugItem']({})
    //  t.ZY_Detail_Show=!t.ZY_Detail_Show;
    },
    tabsIndexClick(tab, event) {
       let t=this;
     
        //    this.params.tabIndex=1
     this.params.page=1
      t.data=[];
        t.getList()
    },
    
    searchB() {
      console.log(123)
      t.params.page=1;
      t.getList()
    },

 
     handlePagination(e){
       
         t.params.page=e;
        t.getList()
        
    },
    getList(){
    
  
  
      if(t.params.tabIndex=='1'){
            let d={
        // keyword:t.params.keyword,
        limit:t.params.limit,
        page:t.params.page,
        start_time:this.params.start_time,
        // status:t.params.tabIndex,
      }
            getDrugHouse(d).then(res=>{
        //  this.params.loading=false;
        console.log(res)
                 if(res.code==200){
                   
                     
                        this.data=res.data.data.lists;
                        for(let i in this.data){
                          if(this.data[i].get_fill.length!=0){
                          this.data[i].fill_id=this.data[i].get_fill[0].id

                          }else{
                             this.data[i].fill_id=''
                          }
                        }
                        console.log(this.data)
                        this.tableTitle=this.tableTitle1
                        this.params.pageCount =res.data.data.count;
                       
                 }
      })
      }else{
            let d={
        // keyword:t.params.keyword,
        limit:t.params.limit,
        page:t.params.page,
        start_time:this.params2.start_time,
         end_time:this.params.end_time
        // status:t.params.tabIndex,
      }
       
          getDrugList(d).then(res=>{
        //  this.params.loading=false;
        console.log(res)
                 if(res.code==200){
                   
                     
                        this.data=res.data.lists;
                        // for(let i in this.data){
                        //   if(this.data[i].get_fill.length!=0){
                        //   this.data[i].fill_id=this.data[i].get_fill[0].id

                        //   }else{
                        //      this.data[i].fill_id=false
                        //   }
                        // }
                        // console.log(this.data)
                        this.tableTitle=this.tableTitle2
                        this.params.pageCount =res.data.count;
                       
                 }
      })
        
      }
      //  this.params.loading=true;
  
      
    },
    handleSelectionChange(e){
        console.log(e)
        this.data2=e
    },
    yijianpeiyao(){
      var arr=[]
      var arr1=[]
      for(var i in this.data2){
        if(this.data2[i].fill_id==''){
             this.$message.error(this.data2[i].name+'批号不能为空');
             return
        }else{
          arr1.push(this.data2[i].fill_id)
          arr.push(this.data2[i].business_id)

        }
      }
    var d={
      business_id:arr.toString(),
      fill_id:arr1.toString(),
      start_time:this.params.start_time,
    }
      batchGenDrug(d).then(res=>{
          if(res.code==200){
            this.$message({
                        message: '操作成功',
                        type: 'success'
                      });
          }
      })
    },
      yijiantuiyao(){
      var arr=[]

      for(var i in this.data2){
     
        
          arr.push(this.data2[i].id)

        
      }
    var d={
      ids:arr.toString(),
      
    }
      batchRefundDrug(d).then(res=>{
          if(res.code==200){
            this.$message({
                        message: '操作成功',
                        type: 'success'
                      });
          }
      })
    },

  },
};
</script>

<style lang="scss" scoped>
/deep/ .el-tabs__nav {
  
  .el-tabs__item {
    border-bottom: 1px solid #e4e7ed;
  }
  .is-active {
    color: #02bf6b;
  }
  .el-tabs__item:hover {
    color: #02bf6b;
  }
}
.search {
  border: 1px solid #f2f2f2;
  margin: 10px 2%;
  padding: 20px;
  border-radius: 10px;
  .s1 {
    width: 600px;
    display: flex;
    button {
      margin-left: 20px;
      margin-bottom: 20px;
      background-color: #02bf6b;
      color: white;
    }
  }
  .selectBai {
    display: flex;
    > div {
      margin-right: 10px;
    }
  }
}

.tableMore {
  /deep/ .el-button--primary {
    color: black;
    background-color: initial;
    border-color: #e2e2e2;
  }
}
.tablesB,.tabsB{margin: 20px 2% 0;}

  .fenye{
    text-align: right;
    margin-top: 20px;   
    
  }
  /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active{
      background-color: #02bf6b;
  }

  
  /deep/ .el-dialog__body {
  // height: 70vh;
  overflow: scroll;
}

</style>